package com.elcom.roaming.module.synthesis.lb.client;


import java.io.UnsupportedEncodingException;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;

import com.elcom.ipc_as_smpp.pdu.IPCPDU;

public class IPCFwc extends IPCPDU {
	public static final int CID_FWC = 0x00000073;
	public static final int MAX_ADDRESS_LEN = 20;
	public static final int MAX_TIME_LEN = 40;
	public static final int MAX_LOCATION_LEN = 20;

	private byte reason;
	private byte rlcCode;
	private byte isTraceList;
	private String calling;
	private String called;
	private String time;
	private String location;

	public byte getReason() {
		return reason;
	}

	public void setReason(byte reason) {
		this.reason = reason;
	}

	public byte getRlcCode() {
		return rlcCode;
	}

	public void setRlcCode(byte rlcCode) {
		this.rlcCode = rlcCode;
	}

	public byte getIsTraceList() {
		return isTraceList;
	}

	public void setIsTraceList(byte isTraceList) {
		this.isTraceList = isTraceList;
	}

	public String getCalling() {
		return calling;
	}

	public void setCalling(String calling) {
		this.calling = calling;
	}

	public String getCalled() {
		return called;
	}

	public void setCalled(String called) {
		this.called = called;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public IPCFwc() {
		super(CID_FWC);
	}

	@Override
	public ChannelBuffer getBody() {
		ChannelBuffer body_buf = ChannelBuffers.dynamicBuffer();

		body_buf.writeByte(reason);
		body_buf.writeByte(rlcCode);
		body_buf.writeByte(isTraceList);

		writeStringPaddingZero(body_buf, calling, MAX_ADDRESS_LEN);
		writeStringPaddingZero(body_buf, called, MAX_ADDRESS_LEN);
		writeStringPaddingZero(body_buf, time, MAX_TIME_LEN);
		writeStringPaddingZero(body_buf, location, MAX_LOCATION_LEN);

		return body_buf;
	}

	@Override
	public void setBody(ChannelBuffer buf) {
		reason = buf.readByte();
		rlcCode = buf.readByte();
		isTraceList = buf.readByte();

		// byte[] calling_buf = new byte[MAX_ADDRESS_LEN];
		// buf.readBytes(calling_buf);
		// calling = new String(calling_buf);
		calling = IPCPDU.readCString(buf.readBytes(MAX_ADDRESS_LEN));

		// byte[] called_buf = new byte[MAX_ADDRESS_LEN];
		// buf.readBytes(called_buf);
		// called = new String(called_buf);
		called = IPCPDU.readCString(buf.readBytes(MAX_ADDRESS_LEN));

		// byte[] time_buf = new byte[MAX_TIME_LEN];
		// buf.readBytes(time_buf);
		// time = new String(time_buf);
		time = IPCPDU.readCString(buf.readBytes(MAX_TIME_LEN));

		// byte[] location_buf = new byte[MAX_LOCATION_LEN];
		// buf.readBytes(location_buf);
		// location = new String(location_buf);
		location = IPCPDU.readCString(buf.readBytes(MAX_LOCATION_LEN));
	}

	private static void writeStringPaddingZero(ChannelBuffer dest, String src,
			int max_len) {
		try {
			byte[] array = src.getBytes("US-ASCII");
			int len = array.length;
			if (array.length > max_len) {
				len = max_len;
			}
			dest.writeBytes(array, 0, len);

			// Padding Zero
			int remain = max_len - len;
			if (remain > 0) {
				dest.writeBytes(new byte[remain]);
			}
		} catch (UnsupportedEncodingException e) {
		}
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		sb.append("{fwc: ");
		sb.append(reason).append("|");
		sb.append(rlcCode).append("|");
		sb.append(isTraceList).append("|");
		sb.append(calling != null ? calling : "").append("|");
		sb.append(called != null ? called : "").append("|");
		sb.append(time != null ? time : "").append("|");
		sb.append(location != null ? location : "");
		sb.append("}");

		return sb.toString();
	}

	public static void main(String[] args) {
		ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
		writeStringPaddingZero(buf, "84903207769", MAX_ADDRESS_LEN);
		writeStringPaddingZero(buf, "9220", MAX_ADDRESS_LEN);
	}
}
